/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.service.ServiceException;
import com.jeesite.modules.aliyun.msg.AliyunSmsUtils;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.entity.KjMemberIntegralHistory;
import com.jeesite.modules.kj.entity.MemberActivity;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.kj.entity.KjMemberJob;
import com.jeesite.modules.kj.dao.KjMemberJobDao;

/**
 * kj_member_jobService
 * @author hzy
 * @version 2021-07-12
 */
@Service
@Transactional(readOnly=true)
public class KjMemberJobService extends CrudService<KjMemberJobDao, KjMemberJob> {

	@Value("${msg.sms.template.attentionSuccess}")
	private String attentionSuccessTpl;
	@Value("${msg.sms.template.attentionFailed}")
	private String attentionFailedTpl;
	/**
	 * 获取单条数据
	 * @param kjMemberJob
	 * @return
	 */
	@Override
	public KjMemberJob get(KjMemberJob kjMemberJob) {
		return super.get(kjMemberJob);
	}

	/**
	 * 查询分页数据
	 * @param kjMemberJob 查询条件
	 * @param kjMemberJob.page 分页对象
	 * @return
	 */
	@Override
	public Page<KjMemberJob> findPage(KjMemberJob kjMemberJob) {
		return super.findPage(kjMemberJob);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param kjMemberJob
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(KjMemberJob kjMemberJob) {
		super.save(kjMemberJob);
	}

	/**
	 * 更新状态
	 * @param kjMemberJob
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(KjMemberJob kjMemberJob) {
		super.updateStatus(kjMemberJob);
	}

	/**
	 * 删除数据
	 * @param kjMemberJob
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(KjMemberJob kjMemberJob) {
		super.delete(kjMemberJob);
	}


	public List<MemberActivity> findMemberActivies(String memberId, String year, String month, int pageNo, int pageSize){
		KjMemberJob where = new KjMemberJob();
		KjMember member = new KjMember();
		member.setId(memberId);
		where.setKjMember(member);
		Page<KjMemberJob> page = new Page(pageNo, pageSize);
		where.setPage(page);
		if(StringUtils.isNotBlank(year) && StringUtils.isNotBlank(month) && Integer.parseInt(year) != 0 && Integer.parseInt(month) != 0){
			Calendar calendar = Calendar.getInstance();
			calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
			where.setCreateDateStr(DateUtils.formatDate(calendar.getTime()));
		}
		return dao.findMemberActivities(where);
	}

	//发送报名审核状态短信
	public void sendAttentionSms(KjMemberJob mj){
		KjMember member = null;
		try {
			member = KjMemberUtil.getCurrentMember(mj.getKjMember().getId());
		} catch (FreezMemberException e) {
			e.printStackTrace();
		}
		if(KjMemberJob.OK.equals(mj.getApproveStatus())){
			authSms(member.getMobile(), attentionSuccessTpl);
		}
		if(KjMemberJob.REJECT.equals(mj.getApproveStatus())){
			authSms(member.getMobile(), attentionFailedTpl);
		}
	}




	public void authSms(String mobile, String tpl){
		Map<String, Object> tplData = new HashMap<>();
		String result = AliyunSmsUtils.sendByTemplate(mobile, tpl, JSON.toJSONString(tplData));
		Map<String, Object> map = JsonMapper.fromJson(result, Map.class);
		// 发送成功
		if (ObjectUtils.toInteger(map.get("result")) == 0){
		}//			 发送失败
		else{
			logger.error(text("短信发送失败{0}", result));
		}
	}
}
